#!/usr/bin/env python
# -*- coding: utf-8  -*-

"""
Cleans Nagios' ndoutils database tables. The purpose of this script is
to be called as a cron-job that reqularily 'empties' the entries in the ndoutils
database, as these tend to fill up the available disk-space.
"""

from sqlalchemy import create_engine
from sqlalchemy import MetaData

VERBOSE = False

if __name__ == "__main__":
    URL = 'mysql://ndoutils:<NDOUTILS_PASSWD>@localhost/ndoutils'

    ctables = ['nagios_logentries', 
                'nagios_systemcommands', 
                'nagios_externalcommands', 
                'nagios_servicechecks',
                'nagios_hostchecks', 
                'nagios_timedevents', 
                'nagios_statehistory']  # names of the tables we are going to 'empty'

    engine = create_engine(URL)
    meta = MetaData()
    meta.reflect(bind=engine)
    for table in meta.sorted_tables:
        if table.name in ctables:
            if VERBOSE:
                print 'emptying ' , table.name
            engine.execute(table.delete())

